import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class DateFormat {
  gainAllDateBetRange(startDate, endDate) {
    let dateArr = [];
    if (!(startDate instanceof Date && endDate instanceof Date)) return dateArr; // 非时间格式返回空数组
    let startTime = startDate.getTime(); // 获取开始日期的毫秒数
    let endTime = endDate.getTime(); // 获取结束日期的毫秒数
    let oneTime = 24 * 60 * 60 * 1000; // 一天的毫秒数
    for (let time = startTime; time <= endTime; ) {
      dateArr.push(this.dateFormat(new Date(time))); // 将格式化后的时间存入结果数组
      time += oneTime; // 每次加一天
    }
    return dateArr;
  }

  dateFormat(date) {
    let year = `${date.getFullYear()}-`;
    let month = `${date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1}-`;
    // let day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
    let day = `${date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()}`;
    return year + month + day;
  }
}
